package org.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/*
 * This class displays all the books that are lister under a 
 * course number in our application
 * */
public class DislayAllBooksFromCourseNumberActivity extends Activity {
	private String CourseNumber = "";

	@SuppressWarnings("unused")
	private TextView book1, book2, book3, book4, book5, textview1;
	private String Book[] = new String[10];
	private String ISBN[] = new String[10];
	int counter = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		String result = "";

		super.onCreate(savedInstanceState);

		setContentView(R.layout.displayallbooksfromcoursenumberlayout);

		book1 = (TextView) findViewById(R.id.book1);
		book2 = (TextView) findViewById(R.id.Book2);
		book3 = (TextView) findViewById(R.id.Book3);
		book4 = (TextView) findViewById(R.id.Book4);
		textview1 = (TextView) findViewById(R.id.textview1);

		Bundle extras = getIntent().getExtras();

		if (extras == null) {
			return;
		}
		CourseNumber = (String) extras.getSerializable("CourseNumber");

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters
				.add(new BasicNameValuePair("CourseNumber", CourseNumber));
		
		//In here we send request to remote database for displaying books related to particular 
		//course number
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/GetBooksDetailsGivingCourseNumber.php");

		try {
			httppost.setEntity(new UrlEncodedFormEntity(postParameters));

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();


			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

			result = sb.toString();
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		try {
			JSONArray jArray = new JSONArray(result);

			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				Log.i("log_tag", " ISBN: " + json_data.getString("ISBN")
						+ ", Author: " + json_data.getString("Author")
						+ ", Title: " + json_data.getString("Title")
						+ ", Publisher: " + json_data.getString("Publisher")

				);
				ISBN[counter] = json_data.getString("ISBN");
				Book[counter] = " ISBN: " + json_data.getString("ISBN")
						+ " \n Author: " + json_data.getString("Author")
						+ "\n Title: " + json_data.getString("Title")
						+ "\n Publisher: " + json_data.getString("Publisher");
				counter++;
			}

		} catch (JSONException e) {
								
			System.out.println("NO RECORDS FOUND");
		}


		textview1.setText("Book Details for " + CourseNumber);
		book1.setText(Book[0]);
		book2.setText(Book[1]);
		book3.setText(Book[2]);
		book4.setText(Book[3]);

		for (int i = 0; i < counter; i++)
			System.out.println("BOOK1: " + Book[i] + "\n");
	}
	
	
	//the back button functionality
	public void Back(View view) throws Exception {
		Intent i = new Intent(this, BuySell8Activity.class);

		startActivity(i);
	}

	//Once the item is clicked the Activity is switched to GetSingleBookAndStudentDetailsActivity 
	
	
	public void onClick1(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[0]);
		startActivity(i);
	}

	public void onClick2(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[1]);
		startActivity(i);
	}

	public void onClick3(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[2]);
		startActivity(i);
	}

	public void onClick4(View view) throws Exception {
		Intent i = new Intent(this,
				GetSingleBookAndStudentDetailsActivity.class);
		i.putExtra("CourseNumber", CourseNumber);
		i.putExtra("ISBN", ISBN[3]);
		startActivity(i);
	}

}